package com.chixing.day15_map;

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {
    //根据key排序
    TreeMap<String,Integer> map = new TreeMap<>();
    map.put("shuma",2000);
    map.put("fushi",2000);
    map.put("shiin",2000);
    map.put("jiadian",2000);
    System.out.println(map);  //{fushi=2000, jiadian=2000, shiin=2000, shuma=2000}根据key排序

    TreeMap<Product,Integer> productMap = new TreeMap<>();
    Product product1 =  new Product(1,"农夫山泉",2);
    Product product2 =  new Product(2,"荔枝",21);
    Product product3 =  new Product(3,"水溶",3);
    Product product4 =  new Product(3,"水溶",3);
    Product product5 =  new Product(4,"水溶",5);
    productMap.put(product1,12);
    productMap.put(product2,121);
    productMap.put(product3,122);
    productMap.put(product4,122);
    productMap.put(product5,52);
    System.out.println(productMap);

    TreeMap<Product,Integer> treeMap = new TreeMap<>(new Comparator<Product>() {
        @Override
        public int compare(Product o1, Product o2) {
            return 0;
        }
    });
    treeMap.put(product1,100);
    treeMap.put(product2,200);
    treeMap.put(product3,300);








    }
}

class Product implements Comparable<Product>{
    private int proId;
    private String proName;
    private float proPrice;
    private String proSale;

    public Product(){}
    public Product(int proId, String proName, float proPrice) {
        this.proId = proId;
        this.proName = proName;
        this.proPrice = proPrice;

    }

    public int getProId() {
        return proId;
    }

    public void setProId(int proId) {
        this.proId = proId;
    }

    public String getProName() {
        return proName;
    }

    public void setProName(String proName) {
        this.proName = proName;
    }

    public float getProPrice() {
        return proPrice;
    }

    public void setProPrice(float proPrice) {
        this.proPrice = proPrice;
    }

    @Override
    public String toString() {
        return "Product{" +
                "proId=" + proId +
                ", proName='" + proName + '\'' +
                ", proPrice=" + proPrice +
                '}';
    }

    //定义比较规则，实现先后顺序的比较

    //先比较name的字母顺序(字典顺序)
    // 若name相同 比较价格，
    // 若价格相同 比较id
   /* @Override
    public int compareTo(Product o) {
        if (this == o) return 0;
        if (this.proName.compareTo(o.proName) == 0){  //"abc" "abc" ,name相同，继续比较price
            if (this.proPrice == o.proPrice){  //继续比较id
                return this.proId - o.proId;
            }else{
                return (Float.valueOf(this.proPrice).compareTo((o.proPrice)));
            }

        }else
            return this.proName.compareTo(o.proName);

    }*/

    @Override
    public int compareTo(Product o) {
        if(this ==o) return 0;

        if(this.proName.compareTo(o.proName) !=0)  // "abc" "abc" , 继续比较 price
            return this.proName.compareTo(o.proName);
        if (this.proPrice != o.proPrice)  // 继续比较id
            return (Float.valueOf(this.proPrice).compareTo(o.proPrice));
        return this.proId - o.proId;

    }
}